1 using System;
2 using
UnityEngine;
3 using
System.Collections;
4
5 ///
<summary>
6 ///
This script automatically connects to Photon (using the settings file),
7 ///
tries to join a random room and creates one if none was found (which is ok).
8 ///
</summary>
9 public
class ConnectAndJoinRandom : Photon.MonoBehaviour
10 {

11     ///
<summary>Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts.</summary>
12     
public bool AutoConnect = true;
13
14     
public byte Version = 1;
15
16     ///
<summary>if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings()</summary>
17     
private bool ConnectInUpdate = true;
18
19     
public virtual void Start()
20     {
21         PhotonNetwork.autoJoinLobby =
false; // we join randomly. always. no need to join a lobby to get the list of rooms.
22     }
23
24     
public virtual void Update()
25     {
26         
if (ConnectInUpdate && AutoConnect && !PhotonNetwork.connected)
27         {
28             Debug.Log(
"Update() was called by Unity. Scene is loaded. Let's connect to the Photon Master Server. Calling: PhotonNetwork.ConnectUsingSettings();");
29
30             ConnectInUpdate =
false;
31             PhotonNetwork.ConnectUsingSettings(Version +
"."+Application.loadedLevel);
32         }
33     }
34
35     
// to react to events "connected" and (expected) error "failed to join random room", we implement some methods. PhotonNetworkingMessage lists all available methods!
36
37     
public virtual void OnConnectedToMaster()
38     {
39         
if (PhotonNetwork.networkingPeer.AvailableRegions != null) Debug.LogWarning("List of available regions counts " + PhotonNetwork.networkingPeer.AvailableRegions.Count + ". First: " + PhotonNetwork.networkingPeer.AvailableRegions[0] + " \t Current Region: " + PhotonNetwork.networkingPeer.CloudRegion);
40         Debug.Log(
"OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");
41         PhotonNetwork.JoinRandomRoom();
42     }
43
44     
public virtual void OnPhotonRandomJoinFailed()
45     {
46         Debug.Log(
"OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
47         PhotonNetwork.CreateRoom(
null, new RoomOptions() { maxPlayers = 4 }, null);
48     }
49
50     
// the following methods are implemented to give you some context. re-implement them as needed.
51
52     
public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)
53     {
54         Debug.LogError(
"Cause: " + cause);
55     }
56
57     
public void OnJoinedRoom()
58     {
59         Debug.Log(
"OnJoinedRoom() called by PUN. Now this client is in a room. From here on, your game would be running. For reference, all callbacks are listed in enum: PhotonNetworkingMessage");
60     }
61
62     
public void OnJoinedLobby()
63     {
64         Debug.Log(
"OnJoinedLobby(). Use a GUI to show existing rooms available in PhotonNetwork.GetRoomList().");
65     }
66 }


This script automatically connects to Photon (using the settings file),

tries to join a random room and creates one if none was found (which is ok).

Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts.

if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings()

PhotonNetwork.autoJoinLobby = false; we join randomly. always. no need to join a lobby to get the list of rooms.

to react to events "connected" and (expected) error "failed to join random room", we implement some methods. PhotonNetworkingMessage lists all available methods!

the following methods are implemented to give you some context. re-implement them as needed.




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.483 lượt xem

Gõ tìm kiếm nhanh...